
var baseURL = "../../";
var vm = new Vue({
    el: '#cwapp',
    data: {
        showList: true,
        title: null,
        role: {
            // deptId: null,
            // deptName: null
        },
        q: {
            roleName: null,
            page: "1",
            limit: "10"
        },
        page: {},
        multipleSelection: [],
        /*选择权限时用的树形结构*/
        selectMenuList: [],
        /*默认选中的菜单*/
        menuIdList: [],
        defaultProps: {
            children: 'children',
            label: 'name'
        },
    },
    methods: {
        // 监听pagesize改变事件
        handleSizeChange(val) {
            console.log(val);
            vm.q.limit = val.toString();
            this.loadPage(false);
        },
        // 监听页码值改变事件
        handleCurrentChange(val) {
            console.log(val);
            vm.q.page = val.toString();
            this.loadPage(false);
        },
        // 监听选中的表格记录
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection)
        },
        // 载入页面
        loadPage(home) {
            let q = {}
            if (home) {
                q.page = '1'
                this.list(q);
            } else {
                vm.showList = true;
                this.list(vm.q)
            }

        },
        // 获取列表
        list(data) {
            $.ajax({
                type: "POST",
                url: baseURL + 'sys/role/list',
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (r) {
                    if (r.code === 0) {
                        vm.page = r.page
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 获取选中记录的ID
        getSelectedIds: function () {
            var ids = [];
            for (var item of vm.multipleSelection) {
                ids.push(item.roleId)
            }
            return ids;
        },
        // 获取选中的记录列表
        getMenuList: function () {
            $.get(baseURL + "sys/menu/getTreeMenu", function (r) {
                vm.selectMenuList = r.menuList;
            });
        },
        // 查询功能
        query: function () {
            this.loadPage(false)
        },
        // 增加功能
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            // vm.role = {deptName: null, deptId: null};
            vm.getMenuList();
        },
        // 修改功能
        update: function (event) {
            var ids = this.getSelectedIds();
            let roleId;
            if (ids.length === 1) {
                roleId = ids[0];
            } else {
                this.$message.error('请选择一条记录');
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getMenuList();
            vm.getInfo(roleId)

        },
        // 根据角色ID获取信息
        getInfo: function (roleId) {
            $.get(baseURL + "sys/role/info/" + roleId.toString(), function (r) {
                vm.role = r.role;
                vm.menuIdList = r.role.menuIdList;
                console.log(vm.menuIdList)
            });
        },
        // 保存tree树形组件勾选节点
        getCheckedKeys() {
            console.log(this.$refs.tree.getCheckedKeys());
        },
        // 在插入数据库时，如果有某某一个主要字段的值重复，则不插入，否则则插入
        saveOrUpdate: function (event) {
            vm.role.menuIdList = this.$refs.tree.getCheckedKeys();

            var url = vm.role.roleId == null ? "sys/role/save" : "sys/role/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.role),
                success: function (r) {
                    if (r.code === 0) {
                        Vue.prototype.$message({
                            type: 'success',
                            message: '操作成功!'
                        });
                        vm.loadPage(false);
                    } else {
                        Vue.prototype.$message({
                            type: 'error',
                            message: r.msg
                        });
                    }
                }
            });
        },
        // 删除功能
        del: function (event) {
            var ids = this.getSelectedIds();
            let roleIds;
            if (ids.length === 0) {
                this.$message.error('请选择一条记录');
                return;
            } else {
                roleIds = ids;
            }
            this.$confirm('确定要删除选中的记录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/role/delete",
                    contentType: "application/json",
                    data: JSON.stringify(roleIds),
                    success: function (r) {
                        if (r.code === 0) {
                            Vue.prototype.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            vm.loadPage(false);
                        } else {
                            Vue.prototype.$message({
                                type: 'error',
                                message: r.msg
                            });
                        }
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
    },
    created: function () {
        this.loadPage(true);
    }
});

